<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>修改提交数据</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">学号</label>
        <div class="layui-input-block">
            <input type="text"  id="stuID" name="stuID" lay-verify="required" disabled lay-reqtext="学号不能为空" placeholder="请输入学号" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">姓名</label>
        <div class="layui-input-block">
            <input type="text" id="stuName" lay-verify="required" disabled lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">班级</label>
        <div class="layui-input-block">
            <input type="text" id="classesID" lay-verify="required" disabled lay-reqtext="班级不能为空" placeholder="请输入班级" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">提交时间</label>
        <div class="layui-input-block">
            <input type="text" id="datetime" lay-verify="required" disabled lay-reqtext="提交时间不能为空" placeholder="请输入提交时间" value="" class="layui-input">
        </div>
    </div>



</div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/axios.min.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = parent.layui.$;
            var $$ = layui.$;
            var tableID = layui.data("formTable").tableID;

            // console.log("obj",obj);



        // 添加元素
        function add_form_elements(){
            var exists_ele = ["stuID","stuName","classesID","datetime","beizhu"];
            var elems = layui.data("formTable").fields;
            var beizhu = $$("#beizhu");
            var form_e = $$("div.layui-form");
            console.log("form_e", form_e);
            var html;
            var table_fields = layui.data("formTable").fieldMap;
            // console.log("in edit ", table_fields,layui.data("formTable") );
            for (var i in elems){
                console.log("i",i);
                if (exists_ele.indexOf(i) == -1){
                    var label_name = table_fields[i][0];
                    var disabled = '';
                    var value = elems[i];
                    var name = i
                    if (typeof elems[i] === "string"){
                        // var reg = RegExp("^\\d+_"+i);
                        // if ( reg.test(elems[i])){
                        if (table_fields[i][2]=="upload"){
                           value = "已提交";
                           disabled = "disabled";
                           name='';
                        }
                    }
                    html =     `
                    <div class="layui-form-item">
                        <label class="layui-form-label">`+label_name+`</label>
                        <div class="layui-input-block">
                            <input type="text" name="`+name+`"  value="`+value+`"  `+disabled+` class="layui-input">
                        </div>
                    </div>`

                form_e.append(html);
            } else {
                $$("#"+i).val(elems[i]);
                }
            
        }
        form_e.append(`
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>`);
        form.render();
        }
        add_form_elements();



        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert(JSON.stringify(data), {
                title: '通知'
            }, function () {
                var cur =  $(".layui-laypage-skip").find("input").val(); 
                var limit = $(".layui-laypage-limits").find("option:selected").val();
                var data_ = data.field;
                data_["tableID"] = tableID;
                data_["formID"] = -1;
                axios.post('/formData/formTablePost', data_)
                    .then(function (response) {
                        parent.layui.table.reload('formTableId',{
                            where: {
                                page:cur,
                                limit:limit,
                                tableID:tableID
                            }
                            ,page: {
                                curr: cur //重新从第 1 页开始
                            }
                        });
                        layer.msg("数据修改成功！",{icon:1});
                        // 关闭弹出层
                        layer.close(index);
        
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    }).catch(function (error) {
                        layer.msg("数据修改失败！",{icon:2});
                    }); 



            });

            return false;
        });

    });
</script>
</body>
</html>